package foliotracker.tests;

import static org.junit.Assert.assertEquals;

import java.util.Date;

import org.junit.Before;
import org.junit.Test;

import foliotracker.Stock;

public class StockTest
{
	private Stock stock;
	
	@Before
	public void setup()
	{
		stock = new Stock("MSFT", "Microsoft", 10);
	}
	
	@Test
	public void getSymbolTest()
	{
		assertEquals("MSFT", stock.getSymbol());
	}

	@Test
	public void getNameTest()
	{
		assertEquals("Microsoft", stock.getName());
	}
	
	@Test
	public void getPriceTest()
	{
		assertEquals(10, stock.getPrice(), 0.5);
	}
	
	@Test
	public void setPriceTest()
	{
		stock.setPrice(14);
		assertEquals(14, stock.getPrice(), 0.5);
	}
	
	@Test
	public void buySellTest()
	{
		assertEquals(0, stock.getQuantity());
		
		stock.buy(new Date(), 5, 10);
		assertEquals(5, stock.getQuantity());
		
		stock.buy(new Date(), 4, 10);
		assertEquals(9, stock.getQuantity());
		
		stock.sell(2);
		assertEquals(7, stock.getQuantity());
		
		//make sure stocks are being sold across the transactions
		stock.sell(4);
		assertEquals(3, stock.getQuantity());
	}
	
	@Test(expected = IllegalArgumentException.class)
	public void sellExcessiveQuantityTest()
	{
		stock.buy(new Date(), 5, 10);
		stock.sell(10);
	}
	
	@Test
	public void getInitialCostTest()
	{
		stock.buy(new Date(), 5, 10);
		stock.buy(new Date(), 5, 5);
		assertEquals(75, stock.getInitialCost(), 0.5);
	}
	
	@Test
	public void getTotalValueTest()
	{
		stock.buy(new Date(), 5, 10);
		stock.buy(new Date(), 5, 5);
		assertEquals(100, stock.getTotalValue(), 0.5);
	}
	
	@Test
	public void getProfitTest()
	{
		stock.buy(new Date(), 5, 10);
		stock.buy(new Date(), 5, 5);
		assertEquals(25, stock.getProfit(), 0.5);
	}
	
	@Test
	public void getPercentageGainTest()
	{
		stock.buy(new Date(), 5, 10);
		stock.buy(new Date(), 5, 5);
		assertEquals(0.33, stock.getPercentageGain(), 0.01);
	}
}
